package cn.wolfcode.controller;


import cn.wolfcode.domain.Department;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IDepartmentService;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/department")
public class DepartmentController {
    @Autowired
    private IDepartmentService departmentService;
    //给这个方法补充一些信息
    //同时标识这个方法需要部门控制
    //到时候判断的时候也要用到这个注解
    @RequestMapping("/list")
@RequiresPermissions("department:list")
    public String list(Model model, QueryObject qo){
        PageInfo<Department> pageInfo = departmentService.query(qo);
        model.addAttribute("pageInfo",pageInfo);
        return "department/list";
    }
    @RequiresPermissions("department:input")
    @RequestMapping("/input")
    public String input(Long id, Model model){
        if (id!=null) {
            Department department = departmentService.selectOne(id);
            model.addAttribute("department",department);
        }
        return "department/input";
    }
    @RequiresPermissions("department:delete")
    @RequestMapping("/delete")
    public String delete(Long id){

        if (id!=null) {
            departmentService.delete(id);
        }
        return "redirect:/department/list";
    }
    @RequestMapping("saveOrUpdate")
    @RequiresPermissions("department:saveOrrUpdate")
    public String saveOrUpdate(Department department){
        if (department.getId()==null) {
            departmentService.insert(department);
        }else{
            departmentService.update(department);
        }

        return "redirect:/department/list";
    }
}
